// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/map/api/AreaMapLabelGroupApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/map/model/QueryAreaMapLabelGroupListReq'
import OneClass from '@api/map/model/QueryAreaMapLabelGroupDetailReq'
import AddClass from '@api/map/model/InsertAreaMapLabelGroupReq'
import EditClass from '@api/map/model/UpdateAreaMapLabelGroupReq'
import DelClass from '@api/map/model/DeleteAreaMapLabelGroupReq'
import BaseClass from '@api/map/model/AreaMapLabelGroupDto'
import ChildClass from '@api/map/model/AreaMapLabelGroupPropertyDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const CHILDREN = [ChildClass, 'properties', 1]
const BASE = [BaseClass, 'content', null, (dto, obj) => CORE.grpcDTO(dto, obj, CHILDREN)]

const transList = (arr) => {
  const trans = (item, key) => item[key] && item[key] !== '0' ? +item[key].slice(0, -1) : null // 只有 % 前面的浮点数字
  arr.forEach((item) => {
    item.detailId = item.deviceId = item.id // 详情 ID
    item.left = trans(item, 'longitude')
    item.top = trans(item, 'latitude')
    item.properties.forEach((prop) => {
      prop.areaMapId = item.areaMapId
      prop.areaMapLabelGroupId = item.labelGroupId
    })
  })
  return arr
}

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  validParam(param) {
    return param.areaMapId
  },
  transParam(param) {
    return { ...param, areaId: '' }
  },
  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryAreaMapLabelGroupList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },
  transList,

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne(one, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, one)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryAreaMapLabelGroupDetail', DTO)
    return { ...one, ...resp.content }
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertAreaMapLabelGroup', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateAreaMapLabelGroup', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del(one, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, one)
    await CORE.grpc(TIP, CLIENT, 'deleteAreaMapLabelGroup', DTO)
    return CORE.grpcOK(TIP)
  },
}
